const app = getApp();


Page({
    data: {
    },
    onLoad:function () {
        this.login();
    },
    login:function () {
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                console.log(res.code)

                if (res.code) {
                    wx.showLoading({
                        title: '登录中，请稍侯',
                    });
                    var that = this;
                    wx.request({
                        url: app.globalData.APIURL + '/vending-admin/admin/login/isOpenid',
                        data: {
                            uuid: 'miniprogram',
                            code: res.code,
                            type:1,
                        },
                        // method:'GET',
                        header: {
                            'content-type': 'application/json' // 默认值
                        },
                        success: function (res) {

                            wx.hideLoading();
                            console.log(res.data)
                            if(app.requestEro(res.data.success,res.data.msg)){

                                wx.setStorageSync('userInfo', res.data.user);
                                wx.setStorageSync('token', res.data.token);
                                app.gohome();
                            }
                        },
                        fail: function (err) {
                            console.log(err);
                            wx.hideLoading();
                            app.globalData.needTryagain=true;
                        }

                    })
                } else {
                    console.log('登录失败！' + res.errMsg)
                }


            }
        })
    }
})